import log from '@/utils/log';
import { getDiff } from '@/utils/tools';
import dayjs from 'dayjs';
import { pmUserList } from '../configs/tapd';
import { getContent, sendMsg } from '../dingtalk/func';
/**
 * 任务处理
 */
const taskHandler = async (type: string, event: TapdTaskEventType) => {
  const { id, name, owner, object_type: objectType, due, custom_field_one: cfone }: TapdTaskEventType = event;

  // 判断是需求
  if (objectType !== 'task') {
    log.error('/api/tapd/task只处理任务', JSON.stringify(event));
    throw Error('这里只处理任务。');
    return;
  }
  const currentDate = dayjs(cfone);
  if (type === '1') {
    const differ: number = getDiff(currentDate, dayjs(due));
    // 任务到达结束时间
    if (differ >= 0) {
      let list = owner;
      // 超过2天，通知项管
      if (differ >= 2) {
        list = list.concat(pmUserList);
      }
      const content = getContent('该任务已到达预计结束时间，请及时填写花费并关闭任务！', owner, []);
      await sendMsg(id, list, name, content, 'tasks');
    }
  }
};

export default taskHandler;
